import { defineStore } from "pinia";
const useTagsViewStore = defineStore('tags-view', {
  state: () => ({
    visitedViews: [
      {
        path: '/index',
        component: () => import('@/views/admin/wel/index.vue'),
        name: 'Index',
        title: '工作台',
        meta: {
          title: '首页',
          icon: '',
          noCache: false,
          affix: true,
        },
        query: {},
        fullPath: '/index'
      }
    ],
    cachedViews: [],
    iframeViews: []
  }),
  actions: {
    addView(view) {
      this.addVisitedView(view)
    },
    delView(view){
      return new Promise((resolve) => {
        this.delVisitedView(view)
        resolve({
          visitedViews: [...this.visitedViews]
        })
      })
    },
    addVisitedView(view) {
      if (this.visitedViews.some(item => item.path === view.path)) return
      this.visitedViews.push(Object.assign({}, view, {
        title: view.meta.title || 'no-name'
      }))
    },
    delVisitedView(view){
      return new Promise((resolve) => {
        for (const [i, v] of this.visitedViews.entries()) {
          if (v.path === view.path) {
            this.visitedViews.splice(i, 1)
          }
        }
        resolve([...this.visitedViews])
      })
      
    },
    updateVisitedView(view) {
      for (let v of this.visitedViews) {
        if (v.path === view.path) {
          v = Object.assign(v, view)
          break
        }
      }
    }
  }
})
export default useTagsViewStore